// pages/school/school.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    e_monitor_install: [
      {
        value: 0,
        name: '未安装',
        selected:false
      },
      {
        value: 1,
        name: '已安装',
        selected:false
      },
    ],
    typeArr: [
      { value: 0, name: '请选择' },
      { value: 1, name: '幼儿园' },
      { value: 2, name: '小学' },
      { value: 3, name: '成人小学' },
      { value: 4, name: '普通初中' },
      { value: 5, name: '职业初中' },
      { value: 6, name: '成人初中' },
      { value: 7, name: '普通高中' },
      { value: 8, name: '职业高中' },
      { value: 9, name: '成人高中' },
      { value: 10, name: '普通大学' },
      { value: 11, name: '本科' },
      { value: 12, name: '大专' },
    ],
    typeIdx: 0,
    flag: 0,
    chooseLoaction: 0,
    location: '',
    longitude: 123.287682,
    latitude: 44.124004,
    rawData: [],//后台获取的原始数组
    multiObjArray: [], //整理后在页面循环的数组
    multiIndex: [0, 0],
    flag1: 0,
    comArr: [],
    gridArr: [],
    newArr: [],
    g_id: '',
    c_id: '',
    id:''
  },

  onLoad(opts) {
    console.log(opts)
    this.setData({
      id: opts.id
    })
    wx.showLoading({
      title: '加载中...',
    })
  },
  
  onShow() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];   //上一页
    prevPage.setData({
      status: 1
    })
    this.loadInfo()
  },

  loadInfo() {
    let that = this
    let access_token = wx.getStorageSync('access_token')
    let id = that.data.id
    wx.request({
      url: 'https://baokang.wshoto.com/wx/basicinfo',
      data: {
        access_token: access_token,
        type: 5,
        id: id
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 1) {
          that.setData({
            flag1:1,
            listInfo: res.data.info,
            typeIdx:res.data.info.type
          })
          console.log(res.data.info.e_monitor_install)
          if (res.data.info.e_monitor_install == 0) {
            that.data.e_monitor_install[0].selected = true;
            that.data.e_monitor_install[1].selected = false
            that.setData({
              e_monitor_install: that.data.e_monitor_install
            })
          } else {
            that.data.e_monitor_install[0].selected = false;
            that.data.e_monitor_install[1].selected = true
            that.setData({
              e_monitor_install: that.data.e_monitor_install
            })
          }
        }
        wx.hideLoading()
      }
    })
  },

  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    let data = {
      multiIndex: this.data.multiIndex,
      multiObjArray: this.data.multiObjArray
    }
    data.multiIndex[e.detail.column] = e.detail.value
    switch (e.detail.column) {
      case 0:
        for (let i in this.data.comArr) {
          if (data.multiIndex[0] == i) {
            data.multiObjArray[1] = this.data.newArr[i]
          }
        }
        data.multiIndex[1] = 0;
        break
    }
    this.setData(data)
  },

  bindMultiPickerChange(e) {
    console.log(e)
    let comIdx = e.detail.value[0]
    let multiIndex = e.detail.value[1]
    let c_id = this.data.rawData[comIdx].c_id
    let g_id = this.data.gridArr[comIdx][multiIndex].g_id
    this.setData({
      multiIndex: e.detail.value,
      g_id: g_id,
      c_id: c_id,
      flag: 1
    })
  },

  typeChange(e) {
    console.log(e)
    this.setData({
      typeIdx: e.detail.value,
      flag1: 1
    })
    console.log(this.data.typeArr[this.data.typeIdx].value)
  },

  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    let item1 = this.data.e_monitor_install
    for (let i = 0, len = item1.length; i < len; ++i) {
      item1[i].checked = item1[i].value === e.detail.value
    }
    this.setData({
      item1
    })
  },

  formSubmit(e) {
    console.log(e)
    console.log(this.data.typeArr[this.data.typeIdx].value)
    let { s_name, master, cover_area, master_phone, security_master, security_master_phone, class_num, teacher_num, student_num, party_num, party_clerk, party_clerk_phone, labour_union_master, labour_union_master_phone, labour_vip_num, protecter_num, protecter_master, protecter_master_phone, culture_remark, defire_machine, desteal_machine, duty_room_phone, e_monitor_install, address } = e.detail.value
    let access_token = wx.getStorageSync('access_token')
    let { c_id, g_id } = this.data
    let type = this.data.typeArr[this.data.typeIdx].value
    // let x_coordinate = this.data.longitude
    // let y_coordinate = this.data.latitude
    // if(this.data.c_id == '' || this.data.g_id == ''){
    //   wx.showToast({
    //     title: '所属社区不能为空！',
    //     icon:'none'
    //   })
    //   return false
    // }
    if(e.detail.value.s_name == ''){
      wx.showToast({
        title: '学校名称不能为空！',
        icon:'none'
      })
      return false
    }
    if(type == ''){
      wx.showToast({
        title: '请选择学校类型！',
        icon:'none'
      })
      return false
    }
    if(master == ''){
      wx.showToast({
        title: '负责人姓名不能为空！',
        icon:'none'
      })
      return false
    }
    if(class_num == ''){
      wx.showToast({
        title: '班级数额不能为空！',
        icon:'none'
      })
      return false
    }
    if(address == ''){
      wx.showToast({
        title: '地址不能为空！',
        icon:'none'
      })
      return false
    }
    if(student_num == ''){
      wx.showToast({
        title: '学生人数不能为空！',
        icon:'none'
      })
      return false
    }
    if(teacher_num == ''){
      wx.showToast({
        title: '教师人数不能为空！',
        icon:'none'
      })
      return false
    }
    wx.showLoading({
      title: '正在提交...',
      mask: true
    })
    if(this.data.id){
      wx.request({
        url: 'https://baokang.wshoto.com/wx/schooladd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          id:this.data.id,
          access_token: access_token,
          type: type,
          s_name: s_name,
          student_num: student_num,
          cover_area: cover_area,
          master: master,
          master_phone: master_phone,
          security_master: security_master,
          security_master_phone: security_master_phone,
          class_num: class_num,
          teacher_num: teacher_num,
          labour_vip_num: labour_vip_num,
          party_num: party_num,
          party_clerk: party_clerk,
          party_clerk_phone: party_clerk_phone,
          labour_union_master: labour_union_master,
          labour_union_master_phone: labour_union_master_phone,
          labour_vip_num: labour_vip_num,
          protecter_num: protecter_num,
          protecter_master: protecter_master,
          protecter_master_phone: protecter_master_phone,
          culture_remark: culture_remark,
          defire_machine: defire_machine,
          desteal_machine: desteal_machine,
          duty_room_phone: duty_room_phone,
          e_monitor_install: e_monitor_install,
          address: address,
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 1) {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })
    } else {
      wx.request({
        url: 'https://baokang.wshoto.com/wx/schooladd',
        method: 'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          access_token: access_token,
          // c_id: c_id,
          // g_id: g_id,
          // x_coordinate:x_coordinate,
          // y_coordinate:y_coordinate,
          type: type,
          s_name: s_name,
          student_num: student_num,
          cover_area: cover_area,
          master: master,
          master_phone: master_phone,
          security_master: security_master,
          security_master_phone: security_master_phone,
          class_num: class_num,
          teacher_num: teacher_num,
          labour_vip_num: labour_vip_num,
          party_num: party_num,
          party_clerk: party_clerk,
          party_clerk_phone: party_clerk_phone,
          labour_union_master: labour_union_master,
          labour_union_master_phone: labour_union_master_phone,
          labour_vip_num: labour_vip_num,
          protecter_num: protecter_num,
          protecter_master: protecter_master,
          protecter_master_phone: protecter_master_phone,
          culture_remark: culture_remark,
          defire_machine: defire_machine,
          desteal_machine: desteal_machine,
          duty_room_phone: duty_room_phone,
          e_monitor_install: e_monitor_install,
          address: address,
        },
        success: (res) => {
          console.log(res)
          wx.hideLoading()
          if (res.data.code == 1) {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
              success: (res) => {
                if (res.confirm) {
                  wx.navigateBack()
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.msg,
              showCancel: false,
            })
          }
        }
      })
    }
   
  }
})